/**
 * 
 */
package co.fitstart.mobile.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.product.Product;
import co.fitstart.entity.product.ProductType;
import co.fitstart.entity.product.Tag;
import co.fitstart.mobile.service.ProductService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;

/**
 * @author wilson
 *
 */
@Controller
@RequestMapping("")
public class IndexController {

    @Autowired
    private ProductService productService;
    
    /**
     * Index page
     * 
     * @param distributorId
     * @param request
     * @return
     */
    @RequestMapping({"", "/index"})
    @Router(targetUrl = "/products")
    @MobileDetect(autoLogin = false)
    @WechatJsConfig
    public ModelAndView index(@RequestParam(value = "distributorId", required = false) Long distributorId,
                              HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("index");
        
        if(distributorId != null) {
            modelAndView.addObject("distributorId", distributorId);
        }
        
        List<Product> onlineProducts = productService.list(ProductType.ONLINE, Tag.HOT);
        List<Product> offlineProducts = productService.list(ProductType.OFFLINE, Tag.HOT);

        modelAndView.addObject("onlineProducts", onlineProducts);
        modelAndView.addObject("offlineProducts", offlineProducts);
        
        return modelAndView;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/need-investigation", method = RequestMethod.GET)
    public String needInvestigation() {
        return "auth/need-investigation";
    }
}
